/*******************************************************************************
 * File name: 		main.c
 * Description:
 * Project: 		_framework
 * Target: 			LPC1788
 * Compiler: 		arm-none-eabi-gcc
 * Date: 			Jun 26, 2012
 * Author: 			kuba
 * Based on: 		http://www.freddiechopin.info/
 ******************************************************************************/
/*==============================================================================
 Includes test
==============================================================================*/
# include <string.h>
# include "init.h"
# include "board.h"
# include "timer.h"
# include "uart.h"
# include "gpdma.h"
# include "printf.h"

/*==============================================================================
 Defines
==============================================================================*/
# define UART_POLL		0
# define UART_DMA   	1
# define UART_IRQ 		0
# define XFER_SIZE 		14
/*==============================================================================
 Globals
==============================================================================*/
static uint8 src[] = "UART";
static uint8 dst[XFER_SIZE];
static uint8 triggerLvl[] = {1, 4, 8, 14};
/*==============================================================================
 Static function prototypes
==============================================================================*/
static void jtagDelay   	(void);
static void timeout		 	(void);

static struct io led0, switch0;
# if UART_IRQ
static struct uartIrqConf irq;
static void irqTxService 	(void);
static void irqRxService 	(void);
# endif

# if UART_DMA
struct uartDmaConf dma;
static void dmaTxService 	(void);
static void dmaRxService 	(void);
# endif
/*==============================================================================
 Main function
==============================================================================*/
int main(void)
{
	jtagDelay();
	systemInit();
	//SystemInit();

	/* peripheral init */
	switch0 = boardSwitchInit(gpioPORT2, 10);
	led0 = boardLedInit(gpioPORT1, 18);

	uart0Init(uartBAUD9600, uartSTOP_BIT_1, uartPARITY_DISABLE);

# if UART_IRQ
	irq.priority 		= irqLOWEST_PRIORITY;
	irq.txBuff			= src;
	irq.txBuffSize		= (uint32)strlen(src);
	irq.rxBuff 			= dst;
	irq.rxTriggerLvl 	= uartTRIGER_LEVEL_4CHAR;
	irq.txEndService 	= irqTxService;
	irq.rxEndService 	= irqRxService;
	irq.timeout 		= timeout;

	uart0InitIrq(&irq);
	uart0EnableIrqTx();
	uart0EnableIrqRx();
# endif

# if UART_DMA
	gpdmaInit(irqLOWEST_PRIORITY);
	dma.txChnl = gpdmaCHNL0;
	dma.rxChnl = gpdmaCHNL1;
	dma.txEndService = dmaTxService;
	dma.rxEndService = dmaRxService;
	dma.error = timeout;

	uart0InitDma(&dma);
	uart0DmaRx(dst, 8);
# endif

	uint32 i;
	while (1)
	{
		if (boardSwitchPressed(switch0))
		{
			//uart0EnableIrqTx();
			uart0DmaTx(src, strlen(src));
		}
//		for (i = 0; i < 1000000; ++i);
//		boardLedOn(led0);
//		for (i = 0; i < 1000000; ++i);
//		boardLedOff(led0);
	}
}

/*==============================================================================
 Static function definitions
==============================================================================*/
# if UART_DMA
/*------------------------------------------------------------------------------
 function name:   dmaTxService
 description:     uart DMA TX service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void dmaTxService (void)
{
	boardLedToggle(led0);

}

/*------------------------------------------------------------------------------
 function name:   dmaRxService
 description:     uart DMA RX service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void dmaRxService (void)
{
	//boardLedToggle(led0);
	uart0DmaTx(dst, 8);
	uart0DmaRx(dst, 8);
}
# endif

# if UART_IRQ
/*------------------------------------------------------------------------------
 function name:   irqTxService
 description:     uart IRQ TX service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void irqTxService (void)
{
}

/*------------------------------------------------------------------------------
 function name:   irqRxService
 description:     uart IRQ RX service
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void irqRxService (void)
{
	uart0Tx(dst, triggerLvl[irq.rxTriggerLvl]);
	uprintf("\n%s\n", "RX service ok");
	uart0DisableIrqTx();
}
# endif
/*------------------------------------------------------------------------------
 function name:   timeout
 description:     uart timeout
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void timeout	(void)
{
	uart0Tx(dst, 1);
	uprintf("\n%s\n", "TIMEOUT!");
}

/*------------------------------------------------------------------------------
 function name:   jtagDelay
 description:     function provides a delay required by jtag
 parameters:      none
 returned value:  none
------------------------------------------------------------------------------*/
static void jtagDelay (void)
{
   volatile uint32 i;
   for (i = 0; i < 1000000; ++i);
}
/******************************************************************************
* END OF FILE
******************************************************************************/
